<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class M_librarian Extends MY_Model
{
	private static $ctable = 'library_book_category';
	private static $btable = 'library_books';
	
	public function __construct()
	{
		parent::__construct();
		$this->load->helper('text');
	
	}
	
	
	public function fetch_media($type=FALSE,$start=FALSE,$limit=FALSE)
	{
		if($type == FALSE)
		{
			$query = $this->db->select('book_barcode,book_id,book_name,book_desc,book_copies,lbc_name,book_author,book_isbn, media_types.media_type,book_borrowed, book_copies - book_borrowed as book_available')
							  ->join(self::$ctable,'lbc_id = book_category','left')
							  ->join('media_types','id = media_type_id','left')
							  ->limit($start,$limit)
							  ->get(self::$btable);
		}else{
			foreach($type as $k => $v)
			{
				if(!empty($v))
				{
					$search[$k] = $v;
				}
			}
			
			if(isset($search))
			{
			$query = $this->db->select('book_barcode,book_id,book_name,book_desc,lbc_name,book_copies,book_author,book_isbn,media_types.media_type, book_borrowed, book_copies - book_borrowed as book_available')
								  ->or_like($search)
								  ->join(self::$ctable,'lbc_id = book_category','left')
								  ->join('media_types','id = media_type_id','left')
								  ->get(self::$btable);
			}else{
				return FALSE;
			}
		}
		return $query->num_rows() >= 1 ? $query->result() : FALSE;
	}
	
	public function count_all_media($data = FALSE)
	{
		if($data == FALSE)
		{
			return $this->db->count_all(self::$btable);
		}else{
			foreach($data as $k => $v)
			{
				if(!empty($v))
				{
					$search[$k] = $v;
				}
			}
			
			if(isset($search))
			{
				$q = $this->db->select('count(book_id) as total')
						  ->or_like($search)
						  ->get(self::$btable);
				return $q->num_rows() >= 1 ? $q->row()->total : 0;
			}else{
				return 0;
			}
		}
	}
	
	
	public function fetch_categories($start,$limit)
	{
		$cat = $this->db->select('lbc_id,lbc_name,lbc_desc')
						->limit($start,$limit)
						->get(self::$ctable);
		return $cat->num_rows() >= 1 ? $cat->result() : FALSE;
	}
	
	public function count_all_categories()
	{
		return $this->db->count_all(self::$ctable);
	}
	
	
	
	public function get_book_profile($id)
	{
		$query = $this->db->select('accession_number, call_number,book_barcode,media_type_id,book_id,book_author,book_name,book_copies,book_desc,book_category,book_isbn,book_publisher,DATE_FORMAT(book_dop,"%m/%d/%Y") as book_dop',FALSE)
						  ->where('book_id',$id)
						  ->get(self::$btable);
		return $query->num_rows() >=1 ? $query->row() : FALSE;
	}
	
	public function get_category_array()
	{
		$query = $this->db->select('lbc_id,lbc_name')->get(self::$ctable);
		if($query->num_rows() >= 1)
		{
			$options[] = '-- SELECT SUBJECT--';
			foreach($query->result() as $k => $v)
			{
				$options[$v->lbc_id] = $v->lbc_name;
			}
			return $options;
		}else{
			return array(''=>'No Subjects Available');
		}		
	}
	
	public function add_category($data)
	{
		if(isset($data) AND !empty($data))
		{
			$input['lbc_name'] = $data['cat_name'];
			$input['lbc_desc'] = $data['cat_desc'];
			$input['lbc_created'] = NOW;
			$this->db->insert(static::$ctable,$input);
			return $this->db->affected_rows() >=1 ? TRUE : FALSE;
		}else{
			return FALSE;
		}
	}
	
	public function add_books($data)
	{
		$data['book_created'] = NOW;
		$data['book_dop'] = date('Y-m-d',strtotime($data['dop']));
		$this->db->insert(self::$btable,$data);
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}
	
	public function update_book($data,$b_id)
	{
		$this->db->set($data)->where('book_id',$b_id)->update(self::$btable);
		return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
	}
	
	
}